package com.dianxin.secure.controller;

import java.io.IOException;
import java.util.Set;

import com.dianxin.secure.Constant;
import com.dianxin.secure.model.Secure;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.dianxin.secure.controller.annotation.FormModel;
import com.dianxin.secure.persist.RedisDaoInterface;
import com.dianxin.secure.persist.RedisDaoProxy;

@Controller
@RequestMapping(value="/user")
public class UserController {
	private  Log log=LogFactory.getLog(getClass());
	
	private RedisDaoInterface<Secure> redisDao=RedisDaoProxy.createUserRedisDao();
	
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(Model model, @ModelAttribute("secure")Secure secure) {
		return "user/create";
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String createSubmit(Model model,@ModelAttribute("secure")Secure secure) throws IOException {
		redisDao.save(secure);
		return "redirect:list";
	}

	@RequestMapping(value = "/delete")
	public String delete(@ModelAttribute("secure")Secure secure )throws Exception {
		redisDao.delete(secure);
		return "redirect:/user/list";
	}


	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(@RequestParam("name") String name,@RequestParam("path") String path, @ModelAttribute("secure")Secure secure) throws Exception{
		Set<Secure> secures= redisDao.getByName(name);
		for (Secure one : secures) {
			if(one.getPath().equals(path)){
				secure.setAction(one.getAction());
				secure.setName(one.getName());
				secure.setPath(one.getPath());
				break;
			}
		}
		return "user/edit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String editPost(@FormModel("oldSecure")Secure oldSecure,@FormModel("secure")Secure secure) throws Exception{
		redisDao.update(oldSecure, secure);
		return "redirect:/user/list";
	}
	
	@RequestMapping(value = "/list",method=RequestMethod.GET)
	public String getAll(Model model
            ,@RequestParam(value="pageNumber" ,defaultValue = Constant.pageNumber) int pageNumber
            ,@RequestParam(value="pageSize",defaultValue = Constant.pageSize) int pageSize) throws Exception{
		Set<String> secures= redisDao.get(pageNumber,pageSize);
		model.addAttribute("secures", secures);
        model.addAttribute("pageNumber", pageNumber);
        model.addAttribute("pageSize", pageSize);
        int   maxNumber =   redisDao.getNumber();
        model.addAttribute("maxNumber", maxNumber);
		return "user/listAll";
	}
	
	@RequestMapping(value = "/list",method=RequestMethod.GET,params="method=one")
	public String getOneUser(@RequestParam(value ="name") String name,Model model
            ,@RequestParam(value="pageNumber" ,defaultValue = Constant.pageNumber) int pageNumber
            ,@RequestParam(value="pageSize",defaultValue = Constant.pageSize) int pageSize) throws Exception{
		Set<Secure> secures=redisDao.getPage(name, pageNumber, pageSize);
		model.addAttribute("secures", secures);
        model.addAttribute("pageNumber", pageNumber);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("name", name);
        int   maxNumber =   redisDao.getNumberByName(name);
        model.addAttribute("maxNumber", maxNumber);
		return "user/list";
	}
	

}
